package com.alks.function.controller.common;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.service.common.IHolidayService;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author MS
 * @since 2024/9/20下午5:14
 */
@RestController
@ApiRestController("/common/holiday")
@Api(value = "节假日设定", tags = "节假日设定")
@Slf4j
public class HolidayController {
    @Autowired
    private IHolidayService service;

    @WebLogger
    @PostMapping("/add")
    @ApiOperation(value = "新增", tags = "节假日设定")
    public ResponseInfo add(@RequestParam @JsonDeserialize(using = LocalDateDeserializer.class)
                                @ApiParam(value = "日期",required = true) LocalDateTime date) {
        service.add(date);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PostMapping("/delete")
    @ApiOperation(value = "删除", tags = "节假日设定")
    public ResponseInfo delete(@RequestParam @JsonDeserialize(using = LocalDateDeserializer.class)
                               @ApiParam(value = "日期",required = true) LocalDateTime date) {
        service.delete(date);
        return ResponseInfo.ok();
    }

    @WebLogger
    @PostMapping("/query")
    @ApiOperation(value = "查询", tags = "节假日设定")
    public ResponseInfo query(@RequestParam(required = false)@ApiParam("年")Integer year,
                              @RequestParam(required = false)@ApiParam("月份")Integer month) {
        List<LocalDateTime> holidayList = service.query(year,month);
        /*格式化出参*/
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        List<String> dtoList = new ArrayList<>(holidayList.size());
        for (LocalDateTime time : holidayList) {
            dtoList.add(time.format(formatter));
        }
        return ResponseInfo.ok(dtoList);
    }

    @WebLogger
    @PostMapping("/check")
    @ApiOperation(value = "判断目标时间是否为假期", tags = "节假日设定")
    public ResponseInfo check(@RequestParam @JsonDeserialize(using = LocalDateDeserializer.class)
                                  @ApiParam(value = "日期",required = true) LocalDateTime date) {
        boolean check = service.holidayCheck(date);
        if (check) {
            return ResponseInfo.ok("是");
        }
        return ResponseInfo.ok("不是");
    }


}